import { Vec3 } from 'cc';

export class Vector3 {
	/**
	 * 获得两个三维向量的和
	 * @param pos1
	 * @param pos2
	 */
	public static add(pos1: Vec3, pos2: Vec3): Vec3 {
		var outPos: Vec3 = new Vec3();
		Vec3.add(outPos, pos1, pos2);
		return outPos;
	}

	/**
	 * 获得两个三维向量的差
	 * @param pos1
	 * @param pos2
	 */
	public static sub(pos1: Vec3, pos2: Vec3): Vec3 {
		var outPos: Vec3 = new Vec3();
		Vec3.subtract(outPos, pos1, pos2);
		return outPos;
	}

	/**
	 * 三维向量乘以常量
	 * @param pos1 b
	 * @param pos2
	 */
	public static mul(pos: Vec3, scalar: number): Vec3 {
		var outPos: Vec3 = new Vec3();
		Vec3.multiplyScalar(outPos, pos, scalar);
		return outPos;
	}

	/**
	 * 三维向量除常量
	 * @param pos1 b
	 * @param pos2
	 */
	public static div(pos: Vec3, scalar: number): Vec3 {
		var outPos: Vec3 = new Vec3();

		outPos.x = pos.x / scalar;
		outPos.y = pos.y / scalar;
		outPos.z = pos.z / scalar;

		return outPos;
	}

	/**
	 * 判断两个三维向量的值是否相等
	 * @param pos1
	 * @param pos2
	 */
	public static equals(pos1: Vec3, pos2: Vec3): boolean {
		if (pos1.x == pos2.x && pos1.y == pos2.y && pos1.z == pos2.z) {
			return true;
		}

		return false;
	}

	/**
	 * 三维向量的模
	 */
	public static magnitude(pos: Vec3): number {
		return pos.length();
	}

	/**
	 * 三维向量归一化（标准化、统一化）
	 */
	public static normalize(pos: Vec3): Vec3 {
        var outPos: Vec3 = new Vec3(pos.x, pos.y, pos.z);
        
		return outPos.normalize();
	}

	/**
	 * 获得位置pos1，到位置 pos2 的方向；pos1=>pos2
	 */
	public static direction(pos1: Vec3, pos2: Vec3): Vec3 {
		var outPos: Vec3 = new Vec3();
		Vec3.subtract(outPos, pos2, pos1);
		return outPos.normalize();
	}

	/**
	 * 获得两点间的距离
	 * @param pos1
	 * @param pos2
	 */
	public static distance(pos1: Vec3, pos2: Vec3): number {
		return Vec3.distance(pos1, pos2);
	}

	/**
	 * 点乘 
	 * @param dir1
	 * @param dir2
	 */
	public static dot(dir1: Vec3, dir2: Vec3): number {
		var tempDir1: Vec3 = dir1;
		var tempDir2: Vec3 = dir2;
		// console.log(tempDir1,tempDir2);
		// console.log("x:",tempDir1.x * tempDir2.x);

		// console.log("y:",tempDir1.y * tempDir2.y);
		
		

		return tempDir1.x * tempDir2.x + tempDir1.y * tempDir2.y + tempDir1.z * tempDir2.z;
	}

	/**
	 * 叉乘
	 * @param dir1
	 * @param dir2
	 */
	public static cross(dir1: Vec3, dir2: Vec3): Vec3 {
		var i: Vec3 = new Vec3(1, 0, 0);
		var j: Vec3 = new Vec3(0, 1, 0);
		var k: Vec3 = new Vec3(0, 0, 1);

		var tempDir1: Vec3 = new Vec3(dir1.x, dir1.y, dir1.z);
		var tempDir2: Vec3 = new Vec3(dir2.x, dir2.y, dir2.z);

		var iv: Vec3 = i.multiplyScalar(tempDir1.y * tempDir2.z - tempDir2.y * tempDir1.z);
		var jv: Vec3 = j.multiplyScalar(tempDir2.x * tempDir1.z - tempDir1.x * tempDir2.z);
		var kv: Vec3 = k.multiplyScalar(tempDir1.x * tempDir2.y - tempDir2.x * tempDir1.y);

		return iv.add(jv).add(kv);
	}

	/**
	 * 获得两个方向向量的角度
	 * @param dir1
	 * @param dir2
	 */
	public static angle(dir1: Vec3, dir2: Vec3): number {
		// a、b的点乘
		var dotValue = this.dot(dir1.clone().normalize(), dir2.clone().normalize());
		// θ = arccos⁡(a∙b|a||b|)
		// 因为a、b都标准化，a、b的模都为 1 ； 故 θ = arccos⁡(a∙b)
		return (Math.acos(dotValue) / Math.PI) * 180 * Math.sign(dotValue);
	}

	/**
	 * 获得方向a到方向b的角度（带有方向的角度）
	 * @param a
	 * @param b
	 */
	public static dirAngle(a: Vec3, b: Vec3): number {


        
		// a、b的叉乘
		var c: Vec3 = Vector3.cross(a, b);
		// a、b的夹角
		var angle: number = Vector3.angle(a, b);
		// a到b夹角的符号
		var sign = Math.sign(Vector3.dot(c.normalize(), Vector3.cross(b.normalize(), a.normalize())));

		return angle * sign;
	}
}

